#!-*- encoding:utf8 -*-
from django.template import Template, Context, RequestContext
from django.shortcuts import render_to_response
from django.contrib.auth.models import User
from django.contrib.auth.decorators import user_passes_test, login_required
from django.http import Http404, HttpResponseRedirect, HttpResponse
from django.core.paginator import Paginator, InvalidPage, EmptyPage, PageNotAnInteger

from newosmp.monitor.models import Monitor
from newosmp.util import commom
from newosmp.alert.models import AlertBing
from newosmp import settings
import json
import time

#http://ip/service/senddata/?mid=id&time=12444444&data=cpu:12,mem:23
def sendData(request):
    mid = request.GET.get('mid', False)
    timestamps = request.GET.get('time', False)
    data = request.GET.get('data', False)
    print "Receive Mid:%s, time:%s, data: %s" % (mid, timestamps, data)
    if (not mid) or (not timestamps) or (not data):
        return HttpResponse("Data ERROR")
    else:
        save_ret = saveData(mid, timestamps, data)
        check_ret = checkData(mid, timestamps, data)
        if not save_ret:
            print "save data error!,[mid:%s,timestamps: %s, data: %s]" % (mid, timestamps, data)
            return HttpResponse("Save Data ERROR")
        else:
            return HttpResponse("Save Data SUCC")

    
def saveData(mid, timestamps, data):
    ret = False
    m = Monitor.objects.get(id=int(mid))
    if m:
        table = m.table
        tpl = m.tpl
        ret = commom.saveTplData(table, tpl, data, timestamps)
        t = int (time.localtime().tm_min)
        #if t % settings.HostUpdateFreq == 0 and tpl == "host":
        if  tpl == "host":
            commom.updateHost(m, data)
        return ret
    else:
        return False


def checkData(mid, timestamps, data):
    m = Monitor.objects.get(id=int(mid))
    ab = AlertBing.objects.filter(mid=m)
    print "aaaaaaaaaaa"
    if not ab:
        return True
    else:
        ab = ab[0]
    stra = ab.sid
    gid = ab.gid
    ret = commom.checkTplData(data, stra)
    if ret[0] == False:
        print "send alert msg......."
        commom.alert(m , gid, ret[1])
    return True


def getTplData(request):
    mid = request.GET.get('mid', False)
    items = request.GET.get('items', False)
    frm = request.GET.get('frm', False)
    to = request.GET.get('to', False)
    if not mid or not items:
        return HttpResponse("input error")
    if to == False:
        to = int (time.time())
    if frm == False:
        frm = to - 3600 * 24
    item = items.split(':')
    print item
    m = Monitor.objects.get(id=int(mid))
    table = m.table
    ret = commom.getTplData(table, item, frm , to)
    return HttpResponse(json.dumps(ret))


def getChart(requset):
    #mid or table 
    #items   
    #frm
    #to
    #title
    table = requset.GET.get('table', False)
    if not table:
        mid = requset.GET.get('mid', -1)
        m = Monitor.objects.get(id=int(mid))
        if m:
            table = m.table
    items = requset.GET.get('items', False)
    frm = requset.GET.get('frm', False)
    to = requset.GET.get('frm', False)
    title = requset.GET.get('title', "un title")
    div = requset.GET.get('div', False)
    
    if not table or not items or not div:
        return HttpResponse("input error")
    if to == False:
        to = int (time.time())
    if frm == False:
        frm = to - 3600 * 24*3
    item_lst = items.split(':')
    print item_lst
    lst = []
    #ret = commom.getTplData(table, item_lst, frm ,to )
    for item in item_lst:
        if item.find("|") > 0:
            (field, item_title) = item.split("|")
        else:
            field = item
            item_title = item
        ret = commom.getTplData(table, [field, ], frm , to)
        data = json.dumps(ret)
        lst.append((item_title, data))
    return render_to_response("chart.html", locals())


def getTplData(request):
    mid = request.GET.get('mid', False)
    items = request.GET.get('items', False)
    frm = request.GET.get('frm', False)
    to = request.GET.get('to', False)
    if not mid or not items:
        return HttpResponse("input error")
    if to == False:
        to = int (time.time())
    if frm == False:
        frm = to - 3600 * 24
    item = items.split(':')
    print item
    m = Monitor.objects.get(id=int(mid))
    table = m.table
    ret = commom.getTplData(table, item, frm , to)
    return HttpResponse(json.dumps(ret))
